# -*-coding:utf-8 -*-
from rest_framework import filters
from django.core.cache import caches, cache
from django.apps import apps
from django.db.models.query_utils import Q
from django.contrib.auth import get_permission_codename


class IsOwnerFilterBackend(filters.BaseFilterBackend):

    def init_userContext(self, request):
        self.corp = getattr(request.user, 'mycorp', None)

    def filter_queryset(self, request, queryset, view):
        if request.user.is_superuser:
            return queryset

        self.init_userContext(request)

        conditions = {}
        if self.corp and hasattr(view, "corp_filed"):
            conditions["%s__exact" % view.corp_filed] = self.corp
            return queryset.filter(**conditions)
        return queryset
